!(function(window){
    var xyz = window.xyz;
    if(!xyz){
        throw new Error(" need xyz.js");
    }
    var frameHTML = ' <div class="xyDFrame" id={{dialogId}}>'
                  + '   <div class="xyDHeader">{{title}}{{close}}</div><hr/>'
                  + '   <div class="xyDBody">{{body}}</div>'
                  + '    {{footer}}'
                  + ' </div>',
        closeHTML = "<span class='xyDClose' onclick=xyz.dialog.close('{{dialogId}}')>×</span>";
       
    var _dialog = {
        length:0,
        _openDialog:function(opt){
            _default = opt || {};
            _dialog._create(_default);
        },
        _create:function(opt){
            var xyMask = document.createElement("div"),
                _dialogId = _dialog._getId(),
                ret={},
                type = opt.type || "alert";
            xyMask.id = "xyMask"+_dialog.length;
            opt.title   ? "" : opt.title = "";
            opt.body  ? "" : opt.body = "";
            opt.close && opt.close !== "false" ? "" : closeHTML = "";
            ret = _dialog._handlerType(_dialogId,opt);
            
            xyMask.innerHTML = frameHTML.replace("{{close}}",closeHTML).replace(/{{dialogId}}/g,_dialogId)
            .replace("{{footer}}",typeof ret.footer != "string" ?"":ret.footer)
            .replace("{{title}}",opt.title).replace("{{body}}",opt.body);
            document.body.appendChild(xyMask);
            _dialog.length++;
        },
        _handlerType:function(relId,opt){
            var  footerHTML = '<div class="xyDFooter">'
                  + '     <input type="button" onclick={{yes}} value="{{yesText}}"/>'
                  + '     <input type="button" onclick={{no}} value="{{noText}}"/>'
                  + ' </div>',
				footer = "",
                yes = _dialog._getId(),
                no = _dialog._getId(),
                yesFunc = function(){return 1;},noFunc = function(){return 0;};
			console.log(opt.type === "alert");
            if(opt.type === "confirm"){
                
            }else if(opt.type === "alert"){
				var fts = footerHTML.split("{{noText}}");
				fts[0] = fts[0].substring(0,fts[0].lastIndexOf("<"));
				fts[1] = fts[1].substring(fts[1].indexOf(">")+1);
				footerHTML = fts.join("");
			}else{
				footerHTML = "";
			}
			
			if(opt.yes && typeof opt.yes === "function"){
                        yesFunc = opt.yes;
                }
            if(opt.no && typeof opt.no === "function"){
                        noFunc = opt.no;
               }
                xyz.dialog[yes+"func"] = function(){
                   yesFunc();
					_dialog._close(relId);
                };
                 xyz.dialog[no+"func"] = function(){
                    noFunc();
					 _dialog._close(relId);
                }
			footer = footerHTML.replace("{{yesText}}",opt.yesText || "确定").replace("{{noText}}",opt.noText || "取消")
                .replace("{{yes}}","xyz.dialog['"+yes+"func']()")
                .replace("{{no}}","xyz.dialog['"+no+"func']()");
            return {"footer":footer};
        },
        _getId:function(){
            var id = "";
            for(var i = 0 ; i < 10;i++){
               id += String.fromCharCode(Math.ceil(Math.random()*24)+65);
            }
            return id.toLowerCase();
        },
		_close:function(id){
			var frame = document.getElementById(id),
                mask = frame.parentNode;
                document.body.removeChild(mask);
            _dialog.length--;
		}
    }
    var dialog = {
        openDialog:function(opt){
			console.log(opt);
           _dialog._openDialog(opt);
        },
        close:function(id){
            _dialog._close(id);
            
        }
    } ;
    
  xyz.extend({"dialog":dialog});
})(window);